package service

import (
	"context"
	"ebip/internel/domain"
	"ebip/internel/repository/repo"
	"errors"
	"golang.org/x/crypto/bcrypt"
)

// @Description
// @Author 代码小学生王木木

func NewIAuthSvc(repo repo.IAuthRepo) IAuthSvc {
	return &authService{
		repo: repo,
	}
}

type authService struct {
	repo repo.IAuthRepo
}

func (s *authService) PwdLogin(ctx context.Context, username, password string) (domain.UserInfoDM, error) {
	// TODO: 校验密码
	user, err := s.repo.FindByUserName(ctx, username)
	if err != nil {
		return domain.UserInfoDM{}, err
	}
	err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
	if err != nil {
		// DEBUG
		return domain.UserInfoDM{}, errors.New("密码不对")
	}
	return user, nil
}
